//
//  ProjectsCollectionViewItem.swift
//  JKTools
//
//  Created by 姜奎 on 2024/10/14.
//

import Cocoa

class ProjectCollectionItem: NSCollectionViewItem {

    lazy var projectView: ProjectView = {
        let projectView = ProjectView()
        projectView.backgroundColor = .yellow
        self.view.addSubview(projectView)
        let lead = projectView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor)
        let trail = projectView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor)
        let top = projectView.topAnchor.constraint(equalTo: self.view.topAnchor)
        let bottom = projectView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor)
        self.view.addConstraints([lead,trail,top,bottom])
        return projectView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do view setup here.
    }

    func fillProject(_ project: ProjectInfoBean) {
        projectView.projectNameLab.stringValue = project.projectName
        projectView.currentBranchLab.stringValue = project.sourcePath
    }
}

extension ProjectCollectionItem {

    override func rightMouseDown(with event: NSEvent) {
        let menus = MenuItem.subScriptEnum(isRootProject:true)

        let menu = menuView(menus: menus,target: self, action: #selector(self.action(item:)))
        NSMenu.popUpContextMenu(menu, with: event, for: self.view)
    }

    @objc func action(item: NSMenuItem) {
        guard let menu = IntMenuItem(rawValue: item.tag) else {
            return
        }
        projectView.statusLab.stringValue = ""
print("\(menu.toString())")
//        runUserScript(appleScript: menu.toString().run(consoleOptions: ConsoleOptions(url: self.projectInfo?.sourcePath, path: self.projectInfo?.projectPath)), id: Constants.Id.LauncherApp)

   }
}
